<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Classes extends MY_Controller {
	public $left_menu_min;

	function __construct(){
	parent::__construct();

	  	$parent_id = 6;
		//获取school二级菜单
		$this->load->model('School_manage_menu_model');
	    $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	    // print_r($menu_list);exit;
	    $this->smarty->assign("menu_list", $menu_list);

		$this->left_menu_min = array("menu" => " 教务管理", "curr" => '/classes', "list" => $menu_list);
	  	$this->smarty->assign("left_menu_min", $this->left_menu_min);

		$this->smarty->assign("menu", 'classes');

	}


	/**
	 * Index Page for this controller.
	 * $status（开课状态：1未开课2上课中3已结业）
	 */
	public function index($status=0, $course_id = 0, $campus_id = 0, $class_name = 0, $start_time = 0, $end_time = 0, $limit = 0)
	{
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$status = !empty($post_data['status']) ? $post_data['status'] : 0;
			$course_id = !empty($post_data['course_id']) ? $post_data['course_id'] : 0;
			$campus_id = !empty($post_data['campus_id']) ? $post_data['campus_id'] : 0;
			$class_name = !empty($post_data['class_name']) ? $post_data['class_name'] : 0;
			$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
			$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
		}
		$conditions['status'] = $status;
		$conditions['course_id'] = $course_id;
		$conditions['campus_id'] = $campus_id;
		$conditions['class_name'] = $class_name;
		$conditions['start_time'] = $start_time;
		$conditions['end_time'] = $end_time;
		// $this->user_data['school_id']
		$this->smarty->assign('is_manage',$this->is_manage);
		$this->smarty->assign('conditions',$conditions);
		//获取班级
		$this->load->model('Classo2o_model');
		$classes = $this->Classo2o_model->get_class_by_school($limit, $this->offset, $conditions);
		$this->smarty->assign("classes", $classes['result']);

		//获取班课列表
		$this->load->model('Courseo2o_model');
		$course = $this->Courseo2o_model->get_offline_course($this->user_data['school_id']);
		$this->smarty->assign("course", $course);

		//获取校区
		$this->load->model('Schoolo2o_model');
		$campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
		$this->smarty->assign("campus", $campus);

		$this->load->library('pagination');
		$page_config['base_url'] = '/classes/index/' . $status . '/' . $course_id . '/' . $campus_id . '/' . $class_name . '/' . $start_time . '/' . $end_time;
		$page_config['total_rows'] = $classes['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 9;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);

		$this->smarty->assign("menu", "classes");
		$this->smarty->display("classes/index.html");
	}

	/**
	 * AJAX生成班级邀请码
	 * @param [type] $class_id [description]
	 */
	function change_join_code($class_id){
		//班级详情
		$this->load->model('Classo2o_model');
		$class_info = $this->Classo2o_model->get_class_by_id($class_id);
		$code = getRandChar(6);
		if($class_info['class_join_code']&&$class_info['class_join_code'] == $code){
			$code = getRandChar(6);
		}
		$class = array('class_join_code' => $code );
		$this->db->update('o2o_class',$class,array('id' => $class_id ));
		echo $code;exit;
	}

	/**
	 * 根据分类获取线下课程AJAX
	 * @param  [type] $cate_id [description]
	 * @return [type]          [description]
	 */
	public function get_offline_course_by_cate($cate_id)
	{
		$this->load->model('Courseo2o_model');
		//获取线下课程分类
		$course = $this->Courseo2o_model->get_offline_course_by_cate($cate_id);
		print_r(json_encode($course));exit;
	}

	/**
	 * 根据校区获取教室AJAX
	 * @param  [type] $campus_id [description]
	 * @return [type]            [description]
	 */
	function get_classroom_by_campus($campus_id){
		$this->load->model('Classo2o_model');
		$room = $this->Classo2o_model->get_classroom_by_campus($campus_id);
		print_r(json_encode($room));exit;
	}

	/**
	 *	学生分配
	 *
	 * @param  [type] $class_id [description]
	 * @return [type]           [description]
	 */
	public function student_allot($class_id)
	{
		$this->load->model('School_student_model');

		$post_data = $this->input->post();
		if($post_data){

			//分配学员
			$result = $this->School_student_model->student_allot_class($post_data['student_id'],$class_id);
			if($result>0){
				header("Location:/classes");exit;
			}
		}
		//获取班级信息
		$this->load->model('Classo2o_model');
		$class_info = $this->Classo2o_model->get_class_by_id($class_id);
		$this->smarty->assign("class_info", $class_info);

		//获取未分班的学员信息
		$student_list = $this->School_student_model->get_undistributed_student($this->user_data['school_id'],$class_info['course_id']);
		$this->smarty->assign("student_list", $student_list);

		$this->smarty->assign("menu", "classes");
		$this->smarty->display("classes/student_allot.html");
	}

	function teacher($class_id = 0){
		if($this->is_manage == 0){
			show_message('/classes', "你没有权限管理班级");exit;
		}
		$class_info = get_field_by_where("o2o_class", "*", "id='{$class_id}'");
		if(!$class_info){
			show_message('/classes', "你没有权限添加班级");exit;
		}

		if($_POST){
			$post_data = $this->input->post();
			$update_data = array();
			if(!empty($post_data['lecturer_id'])){
				$lecturer_info = get_field_by_where("o2o_teachers", "teacher_name", "id='{$post_data['lecturer_id']}'");
				$update_data['lecturer_id'] = $post_data['lecturer_id'];
				$update_data['lecturer_name'] = $lecturer_info['teacher_name'];
			}
			if(!empty($post_data['manage_teacher_id'])){
				$manage_teacher_info = get_field_by_where("o2o_teachers", "teacher_name", "id='{$post_data['manage_teacher_id']}'");
				$update_data['manage_teacher_id'] = $post_data['manage_teacher_id'];
				$update_data['manage_teacher'] = $manage_teacher_info['teacher_name'];
			}
			$this->db->update("o2o_class", $update_data, array("id"=>$class_id));
			show_message('/classes', "修改成功");exit;
		}

		//获取老师
    	$this->load->model('Teachers_model');
    	$teachers = $this->Teachers_model->get_teacher_all_by_school($this->user_data['school_id']);
    	$this->smarty->assign("teachers", $teachers);

    	$this->smarty->assign("teachers", $teachers);
    	$this->smarty->assign("class_info", $class_info);
		$this->smarty->display("classes/teacher.html");
	}

	/**
	 * 添加班级
	 * @param string $value [description]
	 */
	public function add($class_id=0)
	{
		if($this->is_manage == 0){
			show_message('/classes', "你没有权限添加班级");exit;
		}
		$this->load->model('Courseo2o_model');
		$this->load->model('Classo2o_model');
		//post data
		$post_data = $this->input->post();

		if($post_data){
      		$has_error = 0;
			if(empty($post_data['class_num'])){
        	$this->smarty->assign("class_num_err", "请填写班级编号");
        	$has_error = 1;
      		}else{
				$this->smarty->assign("class_num", $post_data['class_num']);
			}
			if(empty($post_data['class_name'])){
        		$this->smarty->assign("class_name_err", "请填写班级名");
        		$has_error = 1;
      		}else{
				$this->smarty->assign("class_name", $post_data['class_name']);
			}
			if(empty($post_data['course_id'])){
        		$this->smarty->assign("course_id_err", "请选择班课");
        		$has_error = 1;
      		}else{
				$this->smarty->assign("course_id", $post_data['course_id']);
			}
			if(empty($post_data['class_hour'])){
				$this->smarty->assign("class_hour_err", "请填写总课时");
				$has_error = 1;
			}else{
				$this->smarty->assign("class_hour", $post_data['class_hour']);
			}

			if(empty($post_data['class_hour_time'])){
				$this->smarty->assign("class_hour_time_err", "请填写课时时长");
				$has_error = 1;
			}else{
				$this->smarty->assign("class_hour_time", $post_data['class_hour_time']);
			}


			if(empty($post_data['student_count'])){
        		$this->smarty->assign("class_hour_err", "请填写开班人数");
        		$has_error = 1;
      		}else{
				$this->smarty->assign("student_count", $post_data['student_count']);
			}
			if(empty($post_data['lecturer_id'])){
		        $this->smarty->assign("lecturer_id_err", "请选择主讲老师");
		        $has_error = 1;
	      	}else{
				$this->smarty->assign("lecturer_id", $post_data['lecturer_id']);
			}
			if(empty($post_data['manage_teacher_id'])){
	        	$this->smarty->assign("manage_teacher_id_err", "请选择班主任");
	        	$has_error = 1;
	      	}else{
				$this->smarty->assign("manage_teacher_id", $post_data['manage_teacher_id']);
			}
			if(empty($post_data['start_time'])){
				$this->smarty->assign("start_time_err", "请填写上课起始日");
				$has_error = 1;
			}else{
				$this->smarty->assign("start_time", $post_data['start_time']);
			}
			if(empty($post_data['end_time'])){
				$this->smarty->assign("class_time_err", "请填写上课起始日");
				$has_error = 1;
			}else{
				$this->smarty->assign("end_time", $post_data['end_time']);
			}
			if(empty($post_data['class_time'])){
				$this->smarty->assign("class_time_err1", "请填写上课规律");
				$has_error = 1;
			}else{
				$this->smarty->assign("class_time", $post_data['class_time']);
			}

			//添加班级
			if($has_error==0){
				$this->Classo2o_model->add_class_by_school($this->user_data['school_id'],$post_data);
				header("Location:/classes");exit;
			}
			// print_r($post_data['img_url'][0]);
			// print_r(json_encode($post_data['img_url']));exit;
		}
		//生成班级编号
		$classnum = "";
		$class_count = $this->Classo2o_model->get_last_classnum($this->user_data['school_id']);
		$class_count+=1;
		if($class_count>0){
		 	$bit = 4;//产生7位数的数字编号
	 		$num_len = strlen($class_count);
	 		$zero = '';
		 	for($i=$num_len; $i<$bit; $i++){
			  	$zero .= "0";
	 		}
		 	$real_num = $zero.$class_count;
		 	$classnum = date('Y').$real_num;
		}

		$this->smarty->assign("class_num", $classnum);
		$this->smarty->assign("class_count", $class_count);
		$this->smarty->assign("year_name", date('Y'));

		//获取线下课程分类
		$course_cate = $this->Courseo2o_model->get_offline_cate($this->user_data['school_id']);
		$this->smarty->assign("course_cate", $course_cate);

		//获取校区
		$this->load->model('Schoolo2o_model');
		$campus = $this->Schoolo2o_model->get_school_campus($this->user_data['school_id']);
		$this->smarty->assign("campus", $campus);

		//获取老师
    	$this->load->model('Teachers_model');
    	$teachers = $this->Teachers_model->get_teacher_all_by_school($this->user_data['school_id']);
    	$this->smarty->assign("teachers", $teachers);

		$this->smarty->assign("menu", "classes");
		$this->smarty->display("classes/add.html");
	}



	/**
	 * 获取班级详情
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public function detail($id)
	{
		$school_info = get_field_by_where("o2o_school", "url_wap", "id='{$this->user_data['school_id']}'");
		$this->smarty->assign("school_info", $school_info);
		//班级详情
		$this->load->model('Classo2o_model');
		$class_info = $this->Classo2o_model->get_class_by_id($id);
		$this->smarty->assign("class_info", $class_info);

		//获取班级学员
		$this->load->model('School_student_model');
		$student = $this->School_student_model->get_student_by_class($id);
		$this->smarty->assign("student", $student);

		$this->smarty->assign("menu", "classes");
		$this->smarty->display("classes/class_detail.html");
	}

	function delete_class($id){
		// show_message($this->jump_url, "该班级下面有学员，不能删除");exit;
		$this->load->model('School_student_model');
		$student = $this->School_student_model->get_student_by_class($id);
		if($student){
			show_message($this->jump_url, "该班级下面有学员，不能删除");
			exit;

		}else{
			$this->db->delete('o2o_class',array('id' => $id ));
			header("Location:/classes/");exit;
		}
	}
	/**
	 * 删除班级学员
	 * @param [type] $id [description]
	 */
	function delete_class_student($id,$class_id){
		$this->load->model('School_student_model');
		$this->School_student_model->delete_class_student($id);
		header("Location:/classes/detail/$class_id");exit;
	}

	/**
	 * 评价学员
	 */
	function student_comment(){
		$this->smarty->display("classes/student_comment.html");
	}

	/**
	 * 班级排课
	 * @param  string $value [description]
	 * @return [type]        [description]
	 */
	public function plan($class_id)
	{
		//班级详情
		$this->load->model('Classo2o_model');
		$class_info = $this->Classo2o_model->get_class_by_id($class_id);
		$this->smarty->assign("class_info", $class_info);


		$this->load->model("Class_plan_model");
		$plan_list = $this->Class_plan_model->get_plan_by_class($class_id);
		//获取排课list
		$this->smarty->assign("plan_list", $plan_list);
		// print_r($plan_list);
		$classes_status = '未开课';
		if($class_info['start_time'] > time()){
			$classes_status = '开课中'; //进行中
		}
		if(empty($plan_list)){
			if($class_info['end_time'] < time()){
				$classes_status = '已结束';
			}
		}else{
			$last_plan = end($plan_list);
			if(strtotime($last_plan['class_time']) < time()){
				$classes_status = '已结束';
			}
		}


		$this->smarty->assign("classes_status", $classes_status);
		$this->smarty->assign("menu", "classes");
		$this->smarty->display("classes/class_plan.html");

	}

	function plan_del($plan_id){

		$this->load->model("Class_plan_model");
		$plan = $this->Class_plan_model->get_plan_by_id($plan_id);
		if(empty($plan)){
			header("Location:/classes");exit;
		}
		if($plan['school_id']!=$this->user_data['school_id']){
			header("Location:/classes");exit;
		}
		//删除
		$this->Class_plan_model->delete_plan($plan_id);
		header("Location:/classes/plan/{$plan['class_id']}");exit;
	}

	function volume_list($type = 3, $subject_id = 0, $home_work_name = 0, $classes_id = 0, $status = 0, $start_time = 0, $end_time = 0, $limit = 0){
		$this->left_menu_min['curr'] = '/classes/volume_list/'.$type;
		$this->offset = 10;
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$home_work_name = $post_data['home_work_name'] ? urldecode($post_data['home_work_name']) : 0;
			$subject_id = isset($post_data['subject_id']) ? $post_data['subject_id'] : 0;
			$type = isset($post_data['type']) ? $post_data['type'] : 3;
			$classes_id = !empty($post_data['classes_id']) ? $post_data['classes_id'] : 0;
			$status = !empty($post_data['status']) ? $post_data['status'] : 0;
			$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
			$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
		}
		$this->load->model("Topics");
		$this->load->model("Test_volumes");
		$conditions = array('subject_id' => $subject_id, 'home_work_name' => $home_work_name, "type" => $type, 'classes_id' => $classes_id, 'status' => $status, 'start_time' => $start_time, 'emd_time' => $end_time);
		$result = $this->Test_volumes->get_volume_publish_list($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/classes/volume_list/' . $type . '/' . $subject_id . '/' . $home_work_name . '/' . $classes_id . '/' . $status . '/' . $start_time . '/' . $end_time;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 10;

		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$subject = $this->Topics->get_subject($this->session->userdata['user_data']['school_id']);
		$this->smarty->assign("subject", $subject);
		$this->load->model('Classo2o_model');
		$classes = $this->Classo2o_model->get_class_list($this->user_data['school_id']);
		$this->smarty->assign("classes", $classes);
		$this->smarty->assign("test_volume_list", $result['result']);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$this->smarty->assign("time", time());
		$this->smarty->assign("type", $type);
		$this->smarty->display("classes/class_home_work_list.html");
	}

	function add_home_work($subject_id = 0, $test_volume_name = '', $end_time = 0, $prompt = '', $classes = ''){
		$this->load->model("Topics");
		$school_id = $this->session->userdata['user_data']['school_id'];
		$teacher_id = $this->session->userdata['user_data']['teacher_id'];

		$post_data = $this->input->post();
		if($post_data){
			if(empty($post_data['template_id'])){
				show_message($this->jump_url, "缺少模版ID");
				exit;
			}

			$template_info = get_field_by_where("tk_test_volume_templates", "*", "id='{$post_data['template_id']}'");

			if(!$template_info){
				show_message($this->jump_url, "组卷模版不存在");
				exit;
			}
			if($template_info['school_id'] != $school_id){
				show_message($this->jump_url, "您无法使用该模版");
				exit;
			}
			$test_home_work['name'] = trim($post_data['name']);
			$test_home_work['school_id'] = $school_id;
			$test_home_work['teacher_id'] = $teacher_id;
			$test_home_work['subject_id'] = intval($post_data['subject_id']);
			$test_home_work['topic_source'] = intval($post_data['topic_source']) + 1;
			$test_home_work['create_topic_type'] = intval($post_data['create_topic_type']);
			$test_home_work['topic_info'] = !empty($post_data['topic']) ? implode(",", $post_data['topic']) : '';
			$test_home_work['chapter_id'] = !empty($post_data['chapter_id']) ? implode(",", $post_data['chapter_id']) : '';
			$test_home_work['template_id'] = intval($post_data['template_id']);
			$test_home_work['end_time'] = intval($post_data['end_time']);
			$test_home_work['prompt'] = $post_data['prompt'];
			$test_home_work['classes'] = implode(",", $post_data['classes']);
			//系统随机选题
			if($post_data['create_topic_type'] == 1){
				$template_content = json_decode($template_info['content'], true);
				foreach ($template_content as $key => $value) {
					for ($i=0; $i < $value['number']; $i++) {
						$rand_data = $this->Topics->get_rand_topic($test_home_work['topic_info']);
						if($rand_data){
							$test_home_work['topic_info'][] .= $rand_data['id'];
						}
					}
				}
				if(count($test_home_work['topic_info']) > 0){
					$test_home_work['topic_info'] = implode(",", $test_home_work['topic_info']);
				}
			}else{//自己选题
				if($template_info['total_topic'] != count($post_data['topic'])){
					// echo '您选的题目数量跟组卷模版不符';exit;
				}
			}
			$letter = array('A','B','C','D');
			$topic_list = $this->pretend_topics($test_home_work);
			// print_r($topic_list);exit;
			$this->smarty->assign("test_volume_data", $test_home_work);
			$this->smarty->assign("topic_list", $topic_list);
			$this->smarty->assign("letter", $letter);
			$this->smarty->display("classes/class_home_work_topic_list.html");
		}else{
			$subject = $this->Topics->get_subject();
			if($subject_id == 0){
				$chapter_list_data = $this->get_chapter_list($subject[0]['id']);
			}else{
				$chapter_list_data = $this->get_chapter_list($subject_id);
			}
			$templates = $this->Topics->get_test_volume_templates($school_id);
			$this->load->model('Classo2o_model');
			$this->smarty->assign("class_list", explode("_", $classes));
			$classes = $this->Classo2o_model->get_class_by_school();
			$this->smarty->assign("classes", $classes);
			$this->smarty->assign("subject", $subject);
			$this->smarty->assign("subject_id", $subject_id);
			$this->smarty->assign("test_volume_name", urldecode($test_volume_name));
			$this->smarty->assign("prompt", urldecode($prompt));
			$this->smarty->assign("end_time", $end_time);
			$this->smarty->assign("chapter_list_data", $chapter_list_data);
			$this->smarty->assign("templates", $templates);
			$this->smarty->assign("menu", "classes");
			$this->smarty->display("classes/class_home_work.html");
		}
	}

	function save_home_work(){
		$post_data = $this->input->post();
		if($post_data){
			if(isset($post_data['analys'])) unset($post_data['analys']);
			$post_data['add_time'] = time();
			$this->db->insert("tk_test_home_works", $post_data);
			show_message("/classes/home_work_list");
		}
	}

	function get_chapter_list($subject_id = 0){
		$conditions = array();
		if($subject_id > 0){
			$conditions['subject_id'] = $subject_id;
		}
		$chapter_list = $this->Topics->get_chapter($conditions);
		$chapter_list_data = array();
		$code = 0;
		if($chapter_list){
			foreach ($chapter_list as $key => $value) {
				if($value['parent_id'] == 0){
					$chapter_list_data[$value['id']] = $value;
					unset($chapter_list[$key]);
				}
			}

			foreach ($chapter_list as $key => $value) {
				if(isset($chapter_list_data[$value['parent_id']])){
					$chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
					unset($chapter_list[$key]);
				}
			}

			if($chapter_list AND $chapter_list_data){
				foreach ($chapter_list_data as $key => $value) {
					if(isset($value['sub'])){
						foreach ($value['sub'] as $k => $v) {
							foreach ($chapter_list as $k1 => $v1) {
								if($k == $v1['parent_id']){
									$chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
								}
							}
						}
					}
				}
			}
		}
		return $chapter_list_data;
	}

	function volume_historys($home_work_id = 0, $type = 3, $name = 0, $limit = 0){
		if($this->input->post()){
			$limit = 0;
			$post_data = $this->input->post();
			$home_work_id = !empty($post_data['home_work_id']) ? $post_data['home_work_id'] : 0;
			$name = !empty($post_data['name']) ? urldecode($post_data['name']) : 0;
			$type = !empty($post_data['type']) ? $post_data['type'] : 0;
		}
		if($home_work_id > 0){
			$volume_info = get_field_by_where("tk_test_volumes_publish", "*", "id='{$home_work_id}' AND school_id = '{$this->user_data['school_id']}'");
			if(!$volume_info){
				show_message($this->jump_url, "数据不存在");exit;
			}

			$this->load->model("Test_volumes");
			$conditions['home_work_id'] = $home_work_id;
			$conditions['name'] = $name;
			$conditions['type'] = $type;
			$history_list = $this->Test_volumes->get_volume_publish_history($limit, $this->offset, $conditions);

			$this->load->library('pagination');
			$page_config['base_url'] = '/classes/volume_historys/' . $home_work_id . '/' . $type . '/' . $name;
			$page_config['total_rows'] = $history_list['count'];
			$page_config['per_page'] = $this->offset;
			$page_config['uri_segment'] = 6;
			$this->pagination->initialize($page_config);
			$pagination = $this->pagination->create_links();
			$this->smarty->assign("menu", "classes");
			$this->smarty->assign("type", $volume_info['type']);
			$this->smarty->assign("history_list", $history_list['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->display("classes/class_home_work_history.html");
		}else{
			show_message($this->jump_url, "非法操作");exit;
		}

	}

	function show_volume($home_work_history_id = 0, $type = 3){
		$home_work_history = get_field_by_where("tk_test_volume_historys", "*", "id='{$home_work_history_id}'");
		if(!$home_work_history){
			show_message($this->jump_url, "数据不存在");exit;
		}

		$home_work_info = get_field_by_where("tk_test_volumes", "id, topic_info, template_id", "id='{$home_work_history['volume_id']}'");
		if(!$home_work_info){
			show_message($this->jump_url, "作业数据不存在");exit;
		}

		$template_info = get_field_by_where("tk_test_volume_templates", "*", "id='{$home_work_info['template_id']}'");
		$template_content = json_decode($template_info['content'], true);
		$this->smarty->assign("template_content", $template_content);

		$home_work_info['uid'] = $home_work_history['uid'];
		$home_work_info['home_work_id'] = $home_work_history['volume_id'];
		$home_work_info['data_id'] = $home_work_history['data_id'];
		$this->load->model("Topics");
		$result = $this->pretend_topics($home_work_info, true, $type, $home_work_history_id, true);
		$topic_list = $result['topic_list'];
		$topic_type_analys = $result['topic_type_analys'];
		$letter = array('A','B','C','D');
		// print_r($topic_list);
		$home_work_info['percentage'] = $home_work_history['percentage'];
		$home_work_info['right_percentage'] = $home_work_history['right_percentage'];
		$home_work_info['fraction'] = $home_work_history['fraction'];
		$home_work_info['begin_time'] = $home_work_history['begin_time'];
		$home_work_info['end_time'] = $home_work_history['end_time'];
		$home_work_info['timeout_time'] = $home_work_history['timeout_time'];
		$home_work_info['status'] = $home_work_history['status'];
		$this->smarty->assign("test_volume_data", $home_work_info);
		$this->smarty->assign("topic_list", $topic_list);
		$this->smarty->assign("topic_type_analys", $result['topic_type_analys']);
		/*print_r($home_work_info);
		print_r($topic_list);*/
		$this->smarty->assign("letter", $letter);
		$this->smarty->assign("home_work_history_id", $home_work_history_id);
		$this->smarty->display("classes/class_show_home_work.html");
	}

	//组装题目
	function pretend_topics($test_home_work = array(), $need_user_result = false, $type = 3, $home_work_history_id = 0, $need_topic_type_analys = false){
		$topic_list = array();
		$topic_type_analys = array(
								"1" => array("name" => "单选题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"2" => array("name" => "多选题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"3" => array("name" => "判断题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"4" => array("name" => "计算分析题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0),
								"7" => array("name" => "不定向选择题", "right_num" => 0, "wrong_num" => 0, "fraction" => 0)
						 		);
		if($test_home_work){
			$topic_info_list = $this->Topics->get_topic_by_id($test_home_work['topic_info']);
			if($topic_info_list){
				foreach ($topic_info_list as $key => $value) {
					$user_answer = '';
					if($need_user_result){
						$where = "uid = '{$test_home_work['uid']}' AND volume_id='{$test_home_work['home_work_id']}' AND data_id = '{$test_home_work['data_id']}' AND topic_id='{$value['id']}' AND type = '{$type}' ";
						if($home_work_history_id > 0){
							$where .= " AND volume_history_id='{$home_work_history_id}'";
						}
						$user_answer = get_field_by_where("tk_test_topic_historys", "*", $where);
						if($user_answer && $need_topic_type_analys){
							if($user_answer['topic_type'] < 8){
								if($user_answer['is_right'] == 1){
									$topic_type_analys[$user_answer['topic_type']]['right_num'] = $topic_type_analys[$user_answer['topic_type']]['right_num'] + 1;
									$topic_type_analys[$user_answer['topic_type']]['fraction'] = $topic_type_analys[$user_answer['topic_type']]['fraction'] + $user_answer['fraction'];
								}else{
									$topic_type_analys[$user_answer['topic_type']]['wrong_num'] = $topic_type_analys[$user_answer['topic_type']]['wrong_num'] + 1;
								}
							}
						}
						if($value['type'] == 4 || $value['type'] == 7){
							$user_answer = json_decode($user_answer['user_answers'], true);
						}elseif($value['type'] == 8 || $value['type'] == 9){
							$user_answer = array("user_answers"=>$user_answer['user_answers'],"fraction"=>$user_answer['fraction'],"feedback"=>$user_answer['feedback']);
						}else{
							$user_answer = explode(",", $user_answer['user_answers']);
						}

					}

					if($value['type'] == 4){
						$analy_result = $this->Topics->get_topic_analys($value['id']);
						if($analy_result){
							$value['result'][$key] = $analy_result;
							foreach ($analy_result as $k => $v) {
								$answer_list = $this->Topics->get_topic_analys_result($v['id']);
								$value['result'][$key][$k]['answer_list'] = $answer_list;
								if($need_user_result){
									$value['result'][$key][$k]['user_answer_list'] = $user_answer ? $user_answer[$v['id']] : '';
								}
							}
							$topic_list[$value['type']][] = $value;
						}
					}else{
						$value['result'] = $this->Topics->get_topic_result_by_id($value['id']);
						if($need_user_result){
							$value['user_answer'] = $user_answer ? $user_answer : '';
						}
						$topic_list[$value['type']][] = $value;
					}
				}
			}
		}
		if($need_topic_type_analys){
			return array("topic_list" => $topic_list, "topic_type_analys" => $topic_type_analys);	
		}else{
			return array($topic_list);
		}
		
	}

	/**
	 * 学校手动添加线下学员
	 * @param string $value [description]
	 */
	public function add_offline_student($class_id)
	{
		$post_data = $this->input->post();
		$this->smarty->assign("class_id", $class_id);

		if($post_data){
			$this->load->model('Student_model');
			$result = $this->Student_model->add_student_user_to_class($post_data,$class_id,$this->user_data['school_id']);
			if($result>0){
				print_r('{"result":"0","msg":"success"}');exit;
				// $this->smarty->display("order/student_info_success.html");
				// exit;
	    }
		}

		$this->smarty->display("classes/offline_student_info.html");
	}

	function delete($ids = 0){
		if($_POST){
			$ids = $this->input->post('ids');
		}else{
			$ids = (array) $ids;
		}
		delete_data($ids, "tk_test_volumes_publish");
		if($this->db->affected_rows() > 0){
			show_message($this->jump_url, "删除成功");
		}else{
			show_message($this->jump_url, "删除失败，请重试！");
		}
	}

	function analys($type = 3, $name = 0, $subject_id = 0, $class_id = 0, $start_time = 0, $end_time = 0, $limit = 0){
		echo $this->user_data['teacher_id'];
		$this->left_menu_min['curr'] = '/classes/analys';
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$this->load->model("Topics");
		if($_POST){
			$post_data = $this->input->post();
			$type = !empty($post_data['type']) ? $post_data['type'] : 3;
			$name = !empty($post_data['name']) ? $post_data['name'] : 0;
			$subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
			$class_id = !empty($post_data['class_id']) ? $post_data['class_id'] : 0;
			$start_time = !empty($post_data['start_time']) ? $post_data['start_time'] : 0;
			$end_time = !empty($post_data['end_time']) ? $post_data['end_time'] : 0;
		}
		$conditions['name'] = $name;
		$conditions['subject_id'] = $subject_id;
		$conditions['class_id'] = $class_id;
		$conditions['type'] = $type;
		$conditions['start_time'] = $start_time;
		$conditions['end_time'] = $end_time;
		if($type == 3){
			$type_name = '作业';
		}else{
			$type_name = '考试';
		}
		$analys_data = $this->Topics->get_analys_list($type, $limit, $this->offset, $conditions);
		// print_r($analys_data);
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['base_url'] = '/classes/analys/' . $type . '/' . $name . '/' . $subject_id . '/' . $class_id . '/' . $start_time . '/' . $end_time;
		$page_config['total_rows'] = $analys_data['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 9;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->load->model("Subjects");
		$subject_list = $this->Subjects->get_subject_list_all($this->user_data['school_id']);
		$this->smarty->assign("subject_list", $subject_list);

		$this->load->model("Classo2o_model");
		$class_data = $this->Classo2o_model->get_class_list($this->user_data['school_id']);
		$class_list = array();
		foreach ($class_data as $k => $v) {
			$class_list[$v['id']] = $v;
		}
		foreach ($analys_data['result'] as $key => $value) {
			$analys_data['result'][$key]['classes_name'] = array();
			if($value['classes'] != ''){
				$class_ids = explode(",", $value['classes']);
				if(count($class_ids) > 0){
					foreach ($class_ids as $k1 => $v1) {
						if(isset($class_list[$v1]['class_name'])){
							$analys_data['result'][$key]['classes_name'][] = $class_list[$v1]['class_name'];
						}
					}
				}
			}
		}
		// print_r($analys_data);

		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("class_list", $class_list);
		$this->smarty->assign("topic_list", $analys_data['result']);
		$this->smarty->assign("menu", "classes");
		$this->smarty->assign("type_name", $type_name);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("classes/analys.html");
	}

	function volume_analys_detail($volume_id, $type = 3, $name = 0, $class_id = 0, $limit = 0){
		$this->left_menu_min['curr'] = '/classes/analys';
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$volume_info = get_field_by_where("tk_test_volumes", "*", "id='{$volume_id}'");
		$template_info = get_field_by_where("tk_test_volume_templates", "*", "id='{$volume_info['template_id']}'");
		$template_content = json_decode($template_info['content'], true);
		
		if(!$volume_info){
			show_message("/classes/volume_analys_detail/$volume_id/$type", "组卷不存在");exit;
		}
		
		$this->load->model("Topics");
		$topic_list = $this->Topics->get_all_topic($volume_info['topic_info']);
		$tpoic_type = array("1" => "单选题", "2" => "多选题", "3" => "判断题", "4" => "计算分析题", "7" => "不定向选择题", "8" => "名词解释", "9" => "简答题");
		$volume_analys_info['total_fraction'] = 0;
		$volume_analys_info['topic_type'] = array();
		foreach ($topic_list as $key => $value) {
			$volume_analys_info['total_fraction'] += $template_content[$value['type']]['fraction'];
			if(isset($volume_analys_info['topic_type'][$value['type']])){
				$volume_analys_info['topic_type'][$value['type']]['num'] += 1;	
			}else{
				$volume_analys_info['topic_type'][$value['type']] = array("name" => $tpoic_type[$value['type']], "num" => 1, "fraction" => $template_content[$value['type']]['fraction']);
			}
		}
		$analys_fraction = get_field_by_where("tk_test_volume_historys", "max(fraction) AS max_fraction, min(fraction) AS min_fraction, avg(fraction) AS avg_fraction", "volume_id='{$volume_id}' AND type='{$type}'");
		$volume_analys_info = array_merge($volume_analys_info, $analys_fraction);
		if($_POST){
			$post_data = $this->input->post();
			$name = !empty($post_data['name']) ? $post_data['name'] : 0;
			$class_id = !empty($post_data['class_id']) ? $post_data['class_id'] : 0;
		}
		$conditions['name'] = $name;
		$conditions['class_id'] = $class_id;
		$analys_data = $this->Topics->get_volume_historys_by_id($volume_id, $type, $limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['base_url'] = '/classes/volume_analys_detail/' . $volume_id . '/' . $type . '/' . $name . '/' . $class_id;
		$page_config['total_rows'] = $analys_data['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 7;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		foreach ($analys_data['result'] as $k => $v) {
			$zg_fraction_data = get_field_by_where("tk_test_topic_historys", "SUM(fraction) AS fraction", "volume_history_id='{$v['id']}' AND type='{$type}' AND topic_type IN(8,9)");
			$analys_data['result'][$k]['zg_fraction'] = !empty($zg_fraction_data['fraction']) ? $zg_fraction_data['fraction'] : 0;

			$kg_fraction_data = get_field_by_where("tk_test_topic_historys", "SUM(fraction) AS fraction", "volume_history_id='{$v['id']}' AND type='{$type}' AND topic_type NOT IN(8,9)");
			$analys_data['result'][$k]['kg_fraction'] = !empty($kg_fraction_data['fraction']) ? $kg_fraction_data['fraction'] : 0;
		}

		$ranking = $this->Topics->get_volume_historys_ranking($volume_id, $type);
		$ranking_list = array();
		$now_rank = 1;
		$now_rank_fraction = 0;
		foreach ($ranking as $key => $value) {
			if($value['fraction'] >= $now_rank_fraction){
			}else{
				$now_rank++;
			}
			$ranking_list[$value['id']] = $now_rank;
			$now_rank_fraction = $value['fraction'];
		}

		$this->load->model('Classo2o_model');
		$classes = $this->Classo2o_model->get_class_list($this->user_data['school_id']);
		$this->smarty->assign("class_list", $classes);

		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("volume_analys_info", $volume_analys_info);
		$this->smarty->assign("ranking_list", $ranking_list);
		$this->smarty->assign("volume_id", $volume_id);
		$this->smarty->assign("type", $type);
		$this->smarty->assign("analys_list", $analys_data['result']);
		$this->smarty->assign("menu", "classes");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("classes/volume_analys_detail.html");
	}


	function volume_topic_analys_detail($volume_id, $type = 3, $chapter_id = 0, $topic_type = 0){
		$this->left_menu_min['curr'] = '/classes/analys';
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$volume_info = get_field_by_where("tk_test_volumes", "*", "id='{$volume_id}'");
		$template_info = get_field_by_where("tk_test_volume_templates", "*", "id='{$volume_info['template_id']}'");
		$template_content = json_decode($template_info['content'], true);
		if(!$volume_info){
			show_message("/classes/volume_analys_detail/$volume_id/$type", "组卷不存在");exit;
		}
		$this->load->model("Topics");
		if($_POST){
			$post_data = $this->input->post();
			$chapter_id = !empty($post_data['chapter_id']) ? $post_data['chapter_id'] : 0;
			$topic_type = !empty($post_data['topic_type']) ? $post_data['topic_type'] : 0;
		}
		$conditions['chapter_id'] = $chapter_id;
		$conditions['topic_type'] = $topic_type;
		$topic_list = $this->Topics->get_all_topic($volume_info['topic_info'], $conditions);
		//获取该组卷被做题的次数
		$test_volume_historys = get_field_by_where("tk_test_volume_historys", "count(id) AS count", "volume_id='{$volume_id}' AND type='{$type}'");
		$volume_historys_count = !empty($test_volume_historys['count']) ? $test_volume_historys['count'] : 0;
		$topic_data = array();
		$tpoic_type = array("1" => "单选题", "2" => "多选题", "3" => "判断题", "4" => "计算分析题", "7" => "不定向选择题", "8" => "名词解释", "9" => "简答题");
		$volume_analys_info['total_fraction'] = 0;
		$volume_analys_info['topic_type'] = array();
		foreach ($topic_list as $key => $value) {
			$volume_analys_info['total_fraction'] += $template_content[$value['type']]['fraction'];
			if(isset($volume_analys_info['topic_type'][$value['type']])){
				$volume_analys_info['topic_type'][$value['type']]['num'] += 1;	
			}else{
				$volume_analys_info['topic_type'][$value['type']] = array("name" => $tpoic_type[$value['type']], "num" => 1, "fraction" => $template_content[$value['type']]['fraction']);
			}
			
			//获取组卷做题时题目的回答次数
			$topic_historys = get_field_by_where("tk_test_topic_historys", "count(id) AS count", "topic_id='{$value['id']}' AND type='{$type}' AND volume_id='{$volume_id}'");

			$topic_historys_count = !empty($topic_historys['count']) ? $topic_historys['count'] : 0;
			$value['answer_num'] = !empty($topic_historys['count']) ? $topic_historys_count / $volume_historys_count * 100 : 0;
			$value['topic_answer_num'] = $topic_historys_count;

			$right_topic_historys = get_field_by_where("tk_test_topic_historys", "count(id) AS count", "topic_id='{$value['id']}' AND type='{$type}' AND volume_id='{$volume_id}' AND is_right = 1");

			$right_topic_historys_count = !empty($right_topic_historys['count']) ? $right_topic_historys['count'] : 0;
			$value['right_answer_num'] = !empty($right_topic_historys['count']) ? $right_topic_historys_count / $volume_historys_count * 100 : 0;
			$value['right_topic_historys_count'] = $right_topic_historys_count;

			$topic_data[$key] = $value;
		}
		$analys_fraction = get_field_by_where("tk_test_volume_historys", "max(fraction) AS max_fraction, min(fraction) AS min_fraction, avg(fraction) AS avg_fraction", "volume_id='{$volume_id}' AND type='{$type}'");
		$volume_analys_info = array_merge($volume_analys_info, $analys_fraction);
		$this->load->model("Subjects");
		$chapter_list = $this->Subjects->get_chapter_list(0,0, array("subject_id" => $volume_info['subject_id']));
		$this->smarty->assign("chapter_list", $chapter_list);
		// print_r($topic_data);
		$this->smarty->assign("volume_id", $volume_id);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("volume_analys_info", $volume_analys_info);
		$this->smarty->assign("type", $type);
		$this->smarty->assign("menu", "classes");
		$this->smarty->assign("topic_data", $topic_data);
		$this->smarty->display("classes/volume_topic_analys_detail.html");
	}

	/*//1：章节详情，2：组卷详情
	function analys_list($uid = 0, $subject_id = 0){
		$this->left_menu_min['curr'] = '/classes/analys';
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		if(empty($uid)){
			show_message("/classes/analys", "请选择一个用户");exit;
		}
		$this->load->model("School_student_model");
		$student_info = $this->School_student_model->get_student_info_by_uid($uid, $this->session->userdata['user_data']['school_id']);
		if(!$student_info){
			show_message("/classes/analys", "该学生数据不存在");exit;
		}
		if($_POST){
			$post_data = $this->input->post();
			$subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
			$uid = !empty($post_data['uid']) ? $post_data['uid'] : 0;
		}
		$this->load->model("Topics");
		$conditions = array('subject_id' => $subject_id, 'uid' => $uid);
		$result = $this->Topics->get_analys_data($conditions);
		$result_data = array();
		if(count($result) > 0){
			foreach ($result as $key => $value) {
				$topic_total = get_field_by_where("tk_topics", "COUNT(id) AS total", "chapter_id = '{$value['cid']}'");
				$value['topic_total'] = $topic_total['total'];
				$result_data[$value['subject_id']]['name'] = $value['s_title'];
				$result_data[$value['subject_id']]['count'] = 0;
				$result_data[$value['subject_id']]['list'][$value['pid']]['name'] = $value['p_title'];
				$result_data[$value['subject_id']]['list'][$value['pid']]['list'][] = $value;
			}
		}
		foreach ($result_data as $k => $v) {
			foreach ($v['list'] as $k1 => $v1) {
				$result_data[$k]['count'] += count($v1['list']);
			}
		}

		$subject = $this->Topics->get_subject($this->session->userdata['user_data']['school_id']);
		$this->smarty->assign("subject", $subject);
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("student_info", $student_info);
		$this->smarty->assign("result_data", $result_data);
		$this->smarty->display("classes/analys_list.html");
	}

	function analys_volume($uid = 0, $title = 0, $limit = 0){
		$this->left_menu_min['curr'] = '/classes/analys';
		$this->smarty->assign("left_menu_min", $this->left_menu_min);
		$this->load->model("Topics");
		if($_POST){
			$post_data = $this->input->post();
			$title = !empty($post_data['title']) ? $post_data['title'] : 0;
		}
		$conditions = array('uid' => $uid, 'title' => $title);

		$result = $this->Topics->get_analys_volume($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$page_config['base_url'] = '/classes/analys_volume/' . $uid . '/' . $title;
		$page_config['total_rows'] = $result['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 5;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("conditions", $conditions);
		$this->smarty->assign("topic_list", $result['result']);
		$this->smarty->assign("menu", "classes");
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("classes/analys_volume.html");
	}*/

	function mark_volume(){
		$post_data = $this->input->post();
		if(!empty($post_data['volume_history_id']) && !empty($post_data['topic_id'])){
			$historys = get_field_by_where("tk_test_topic_historys", "id", "topic_id='{$post_data['topic_id']}' AND volume_history_id='{$post_data['volume_history_id']}'");
			if(!$historys){
				echo 3;exit;
			}
			$this->db->where("topic_id", $post_data['topic_id']);
			$this->db->where("volume_history_id", $post_data['volume_history_id']);
			$this->db->update("tk_test_topic_historys", array("fraction"=>$post_data['fraction'], "feedback" => $post_data['feedback']));

			$topic_historys = get_field_by_where("tk_test_topic_historys", "SUM(fraction) AS fraction", "volume_history_id='{$post_data['volume_history_id']}'");
			$this->db->where("id", $post_data['volume_history_id']);
			$this->db->update("tk_test_volume_historys", array("fraction"=>$topic_historys['fraction']));

			if($this->db->affected_rows() > 0){
				echo 1;
			}else{
				echo 0;
			}
		}else{
			echo 2;
		}
	}

	function test(){
		require_once 'third_party/wxpayApi/wxpay.php';
		$wx_config_data = get_field_by_where("o2o_school_wx_config", "*", "school_id='{$this->user_data['school_id']}'");
		if(!$wx_config_data || empty($wx_config_data['app_id']) || empty($wx_config_data['mch_id']) || empty($wx_config_data['apikey']) || empty($wx_config_data['apiclient_cert']) || empty($wx_config_data['apiclient_key'])){
			show_error_message("/user/orders", "该学校不支持微信支付");
		}
		// print_r($wx_config_data);
		$wx_config['mch_appid'] = $wx_config_data['app_id'];
		$wx_config['mchid'] = $wx_config_data['mch_id'];
		$wx_config['apikey'] = $wx_config_data['apikey'];
		/*$wx_config['sslcertPath'] = $wx_config_data['apiclient_cert'];
		$wx_config['sslkeyPath'] = $wx_config_data['apiclient_key'];*/
		$wx_config['sslcertPath'] = getcwd().'/third_party/wxpayApi/cert/apiclient_cert.pem';
		$wx_config['sslkeyPath'] = getcwd().'/third_party/wxpayApi/cert/apiclient_key.pem';
		$wx_config['rootcaPath'] = '/third_party/wxpayApi/cert/rootca.pem';
		$weixin_pay = new wxpay($wx_config);
		
		$response = $weixin_pay->transfers();
		// var_dump($response);
		/*if(!$response || $response['return_code'] == 'FAIL'){
			show_error("微信统一下单失败，" . $response['return_msg']);exit;
		}*/

	}

	function courseware($class_id = 0, $name = 0, $limit = 0 ){
		$conditions['class_id'] = $class_id;
		if($_POST){
			$post_data = $this->input->post();
			if(!empty($post_data['name'])){
				$name = $post_data['name'];
			}
		}
		$conditions['name'] = $name;
		$this->load->model("Courseo2o_model");
		$courses = $this->Courseo2o_model->get_class_course($limit, $this->offset, $conditions);
		$this->load->library('pagination');
		$page_config['base_url'] = '/classes/courseware/' . $class_id . '/' . $name;
		$page_config['total_rows'] = $courses['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 5;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("conditions", $conditions);

		$this->smarty->assign("class_id", $class_id);
		$this->smarty->assign("courses", $courses);
		$this->smarty->display("classes/courseware.html");
	}

	function upload_course($class_id){
		if(empty($class_id)){
			show_message("/classes", "缺少班级ID");
			exit;
		}
		if($_POST){
			$post_data = $this->input->post();
			if(empty($post_data['name']) || empty($post_data['course_fiel'])){
				show_message("/classes/courseware/".$class_id, "缺少数据｛请检查名称是否填写，文件是否上传成功｝");
				exit;
			}
			$post_data = $this->input->post();
			$course_data['school_id'] = $this->session->userdata['user_data']['school_id'];
   			$course_data['teacher_id'] = $this->session->userdata['user_data']['teacher_id'];
   			$course_data['class_id'] = $class_id;
   			$course_data['name'] = $post_data['name'];
   			$course_data['course_fiel'] = get_qiniu_url($post_data['course_fiel']);
   			$course_data['add_time'] = time();
   			$this->db->insert("o2o_class_courses", $course_data);
   			show_message("/classes/courseware/".$class_id, "课件上传成功");exit;
		}
		$this->smarty->display("classes/upload_course.html");
	}

	function delete_course($id){
		$course_info = get_field_by_where("o2o_class_courses", "*", "id='{$id}'");
		if(!$course_info){
			show_message("/classes", "课件不存在");
			exit;
		}
		if($course_info['school_id'] != $this->session->userdata['user_data']['school_id']){
			show_message("/classes", "该课件不属于当前网校");
			exit;
		}

		if($course_info['teacher_id'] != $this->session->userdata['user_data']['teacher_id']){
			show_message("/classes", "无法删除别人的课件");
			exit;
		}

		$this->db->delete('o2o_class_courses',array('id' => $id ));
		show_message("/classes/courseware/".$course_info['class_id'], "课件删除成功");
	}
}
